﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;


using eCitizen.DataContracts;
using eCitizen.eAlerts.Constants;


namespace eCitizen.eAlerts.Utilities
{
    public class SmsManager
    {
        private int _broadcastoftheDay;
        // This method will serialize the SmsMessages into xml file
        public string Broadcast(SftpConfiguration configuration, SmsMessageList messages,int broadcastoftheDay)
        {
            _broadcastoftheDay = broadcastoftheDay;
            return SerializeSmsMessages(configuration, messages);
        }

        private string SerializeSmsMessages(SftpConfiguration configuration, SmsMessageList messages)
        {
            string filePath = ConstructFileName(configuration);
            XmlSerializer serializer = new XmlSerializer(typeof(SmsMessageList));

            if (File.Exists(filePath)) File.Delete(filePath);
            TextWriter writer = new StreamWriter(filePath);

            serializer.Serialize(writer, messages);

            writer.Close();

            return Path.GetFileName(filePath);
        }

        private string ConstructFileName(SftpConfiguration configuration)
        {
            string filePath, fileName;

            fileName = string.Format("{0}{1}.xml", System.DateTime.Now.ToString(ServiceConstants.DateFormats.YYYYMMDD), String.Format("{0:0000000000}", _broadcastoftheDay));
            filePath = string.Format("{0}\\{1}",configuration.GetLocalSmsInboxFolder(), fileName);

            return filePath;

        }



    }
}
